package com.topvision.myviewtest.mvp.demo3;

import com.topvision.myviewtest.mvp.api.ApiServer;
import com.topvision.myviewtest.mvp.bean.WeatherBean;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Author: lucy
 * Comment: M层 数据层
 * Date: 2017-12-11 13:55
 */

public class RequestModel3 {
    private static final String BASE_URL = "http://www.weather.com.cn/";
    private Call<WeatherBean> mWeatherBeanCall;

    public void request(String cityId, Callback<WeatherBean> callback) {
        OkHttpClient.Builder okHttpClient =
                new OkHttpClient.Builder()
                .readTimeout(10, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient.build())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mWeatherBeanCall = retrofit.create(ApiServer.class).requestWeather(cityId);
        mWeatherBeanCall.enqueue(callback);
    }

    /** 取消网络请求 */
    public void interrupHttp() {
        if (mWeatherBeanCall != null && !mWeatherBeanCall.isCanceled()) {
            mWeatherBeanCall.cancel();
        }
    }
}
